package DroidEye.DAO;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import DroidEye.HelloApplication;
import DroidEye.DAO.DAOInterface.TeacherDAOInterface;
import DroidEye.Dto.Apply;
import DroidEye.Dto.Project;
import DroidEye.Dto.Teacher;
import DroidEye.JavaBean.ApplyBean;
import DroidEye.JavaBean.ProjectBean;
import DroidEye.JavaBean.TeacherBean;
import DroidEye.Mapper.ApplyMapper;
import DroidEye.Mapper.ProjectMapper;
import DroidEye.Mapper.TeacherMapper;

@Service
public class TeacherDAO implements TeacherDAOInterface {
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private ProjectMapper projectMapper;
    
    @Autowired
    private ApplyMapper applyMapper;
    
    @Override
    public TeacherBean queryTeacherAccount(String teacherID) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(teacherID);
        if(teacher == null) {
            return null;
        }
        return custTeacher(teacher);
    }

    @Override
    public boolean changeTeacherPassword(String teacherID, String newTeacherPassword) {
        Teacher param = new Teacher();
        param.setTeacherId(teacherID);
        param.setTeacherPassword(newTeacherPassword);
        int num = teacherMapper.updatePasswordById(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int projectNumber(String teacherID) {
        return projectMapper.queryProjectCount(teacherID);
    }

    @Override
    public boolean addProject(ProjectBean projectBean) {
        Project project = new Project();
        project.setProjectId(projectBean.getProjectID());
        project.setProjectName(projectBean.getProjectName());
        project.setProjectInfo(projectBean.getProjectInfo());
        project.setTeacherId(projectBean.getTeacherID());
        int num = projectMapper.insertSelective(project);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<ProjectBean> searchProjectGroupbyTeacher(String teacherID) {
        List<ProjectBean> projectList = new ArrayList<ProjectBean>();
        List<Project> projects = projectMapper.queryByTeacherId(teacherID);
        if(CollectionUtils.isEmpty(projects)) {
            return projectList;
        }
        return custProjectList(projects);
    }


    @Override
    public List<ApplyBean> queryProjectGroupByProjectId(String project_id) {
        List<ApplyBean> projectList = new ArrayList<ApplyBean>();
        List<Apply> applys = applyMapper.queryApplyByProjectId(project_id);
        if(CollectionUtils.isEmpty(applys)) {
            return projectList;
        }
        return custApplyList(applys);
    }

    @Override
    public boolean addProjectTeach(String studentID,String projectTeach) {
        Apply param = new Apply();
        param.setStudentId(studentID);
        param.setProjectTeach(projectTeach);
        int num = applyMapper.updateTeachByStudentId(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean changeProjectProgress(String studentID, String status) {
        Apply param = new Apply();
        param.setStudentId(studentID);
        param.setProjectProgress(status);
        int num = applyMapper.updateProgressByStudentId(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    // 转换Teacher为TeacherBean
    private TeacherBean custTeacher(Teacher teacher) {
        TeacherBean teacherBean = new TeacherBean();
        teacherBean.setTeacherID(teacher.getTeacherId());
        teacherBean.setTeacherPassword(teacher.getTeacherPassword());
        teacherBean.setTeacherName(teacher.getTeacherName());
        return teacherBean;
    }
    
    // 转换List<Project>为List<ProjectBean>
    private List<ProjectBean> custProjectList(List<Project> projectList){
        List<ProjectBean> projectBeanList = new ArrayList<>();
        for(Project project : projectList) {
            ProjectBean projectBean = custProject(project);
            projectBeanList.add(projectBean);
        }
        return projectBeanList;
    }

    // 转换Project为ProjectBean
    private ProjectBean custProject(Project project){
        ProjectBean projectBean = new ProjectBean();
        projectBean.setProjectID(project.getProjectId());
        projectBean.setProjectName(project.getProjectName());
        projectBean.setProjectInfo(project.getProjectInfo());
        projectBean.setTeacherID(project.getTeacherId());
        return projectBean;
    }
    
    // 转换List<Apply>为List<ApplyBean>
    private List<ApplyBean> custApplyList(List<Apply> applyList) {
        List<ApplyBean> applyBeanList = new ArrayList<>();
        for(Apply apply : applyList) {
            ApplyBean applyBean = custApply(apply);
            applyBeanList.add(applyBean);
        }
        return applyBeanList;
    }

    // 转换Apply为ApplyBean
    private ApplyBean custApply(Apply apply) {
        ApplyBean applyBean = new ApplyBean();
        applyBean.setStudentID(apply.getStudentId());
        applyBean.setProjectID(apply.getProjectId());
        applyBean.setTeacherID(apply.getTeacherId());
        applyBean.setStudentName(apply.getStudentName());
        applyBean.setProjectName(apply.getProjectName());
        applyBean.setTeacherName(apply.getTeacherName());
        applyBean.setProjectContent(apply.getProjectContent());
        applyBean.setProjectProgress(apply.getProjectProgress());
        applyBean.setProjectTeach(apply.getProjectTeach());
        applyBean.setPassStatus(apply.getPassStatus());
        applyBean.setProjectTotalScore(apply.getProjectTotalScore());
        applyBean.setAdjuNumber(apply.getAdjuNumber());
        return applyBean;
    }
    
    public static TeacherDAO getTeacherDAOInstance() {
        return HelloApplication.getBean("teacherDAO", TeacherDAO.class);
    }
}
